﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;

namespace He.Web.manage.olservice
{
    public partial class group_edit : Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }

            if (!Page.IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("editolservice");
                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("addolservice");
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.olservice_group model = new BLL.olservice_group().GetModel(id);
            txtGroupName.Text = model.group_name;
            txtSortId.Text = model.sort_id.ToString();
            chkLock.Checked = model.is_lock == 1 ? true : false;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.olservice_group bll = new BLL.olservice_group();
            Model.olservice_group model = new Model.olservice_group();
            model.group_name = txtGroupName.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = chkLock.Checked == true ? 1 : 0;
            if (bll.Add(model) > 0)
            {
                SaveLogs(string.Format("[在线客服分组管理]增加内容:{0}", model.group_name));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.olservice_group bll = new BLL.olservice_group();
            Model.olservice_group model = bll.GetModel(_id);
            model.group_name = txtGroupName.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_lock = chkLock.Checked == true ? 1 : 0;
            if (bll.Update(model))
            {
                SaveLogs(string.Format("[在线客服分组管理]修改内容:{0}", model.group_name));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("修改信息成功啦！", "group_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("添加信息成功啦！", "group_list.aspx");
            }
        }
    }
}